
from sanic import Sanic
from sanic import response
from sanic.log import logger
from sanic_cors import CORS
from build_db import ExecuteSqlScript
from db_interface import ConnPG

from db_interface.warehouse_table import WarehouseTable
from db_interface.storage_location_table import StorageLocationTable
from db_interface.subject_table import SubjectTable
from db_interface.odd_table import OddTable
from db_interface.sku_account_table import SkuAccountTable
from db_interface.duty_class_table import DutyClassTable
from db_interface.duty_warehouse_table import DutyWarehouseTable
from db_interface.sku_warehouse_data import SkuWarehouseData
from db_interface.move_sku_location import MoveSkuLocation

# from db_interface.sku_attribute_table import SkuAttributeTable

app = Sanic(__name__)
CORS(app)

@app.listener('before_server_start')
async def setup_db(app, loop):
    #创建数据库，sql放在build_db文件夹下面
    ExecuteSqlScript().execute_all_script('build_db')

@app.listener('after_server_start')
async def start_db_pool(app, loop):
    ConnPG(loop=loop)
    logger.debug('Server PostgreSQL successfully started!')

@app.route("/")
async def test(request):
    return response.json({"hello": "world"})


# ############# storage_location 表 #################

@app.route("/add_storage_location")#添加祖节点，本系统为一级分类
async def tadd_ancestor(request):
    # return response.json({"hello": "world"})
    location_name = request.args.get('location_name')
    warehouse_id = request.args.get('warehouse_id')
    result = await StorageLocationTable().add_storage_location(location_name, warehouse_id)
    return response.json(result)

@app.route("/select_warehouse_location")#查看所有祖节点，本系统为一级分类
async def tadd_ancestor(request):
    warehouse_id = request.args.get('warehouse_id')
    result = await StorageLocationTable().select_warehouse_location(warehouse_id)
    return response.json(result)

@app.route("/select_view_sku_account_by_storage_location_id")
async def tadd_ancestor(request):
    storage_location_id = request.args.get('storage_location_id')
    result = await SkuAccountTable().select_view_sku_account_by_storage_location_id(storage_location_id)
    return response.json(result)

# ############# warehouse 表 #################

@app.route("/add_warehouse")#添加库房
async def add_warehose(request):

    warehouse_name = request.args.get('warehouse_name')
    result = await WarehouseTable().add_warehouse(warehouse_name)
    # print(result)
    return response.json(result)

@app.route("/select_all_warehouse")#查看所有库房
async def select_all_warehose(request):

    result = await WarehouseTable().select_all_warehouse()
    # print(result)
    return response.json(result)

@app.route("/select_all_warehouse_by_duty_id", methods=['POST'])#查看所有库房
async def select_all_warehose(request):

    duty_id = request.form.get('duty_id')
    result = await WarehouseTable().select_all_warehouse_by_duty(duty_id)
    # print(result)
    return response.json(result)

@app.route("/del_warehouse", methods=['POST'])#删除warehouse
async def del_warehouse(request):

    warehouse_id = request.form.get('warehouse_id')

    result = await WarehouseTable().del_warehouse(warehouse_id)
    # print(result)
    return response.json(result)

@app.route("/update_warehouse", methods=['POST'])#更新warehouse_name
async def update_warehouse(request):

    warehouse_id = request.form.get('warehouse_id')
    warehouse_name = request.form.get('warehouse_name')


    result = await WarehouseTable().update_warehouse(warehouse_id, warehouse_name)
    # print(result)
    return response.json(result)

# ############# subject 表 #################

@app.route("/add_subject")#添加关系主体项目或公司
async def add_subject(request):

    subject_name = request.args.get('subject_name')
    result = await SubjectTable().add_subject(subject_name)
    # print(result)
    return response.json(result)

@app.route("/select_all_subject")#查看所有主体项目或公司
async def select_all_subject(request):

    result = await SubjectTable().select_all_subject()
    # print(result)
    return response.json(result)

@app.route("/del_subject", methods=['POST'])#删除subject
async def del_subject(request):

    subject_id = request.form.get('subject_id')

    result = await SubjectTable().del_subject(subject_id)
    # print(result)
    return response.json(result)

@app.route("/update_subject", methods=['POST'])#更新subject_name
async def update_subject(request):

    subject_id = request.form.get('subject_id')
    subject_name = request.form.get('subject_name')


    result = await SubjectTable().update_subject(subject_id, subject_name)
    # print(result)
    return response.json(result)

# ################# odd ######################

@app.route("/add_odd", methods=['POST'])#添加odd订单
async def add_odd(request):

    in_or_out = request.form.get('in_or_out')
    subject_id = request.form.get('subject_id')
    sponsor_id = request.form.get('sponsor_id')
    warehouse_id = request.form.get('warehouse_id')
    remark = request.form.get('remark')
    print(in_or_out)
    
    result = await OddTable().add_odd(
        in_or_out, subject_id, sponsor_id, warehouse_id, remark
        )
    # print(result)
    return response.json(result)

@app.route("/receive_odd")#库房订单操作人员接受订单
async def receive_odd(request):

    operator_id = request.args.get('operator_id')
    odd_id = request.args.get('odd_id')

    result = await OddTable().receive_odd(odd_id, operator_id)
    # print(result)
    return response.json(result)

@app.route("/success_odd")#修改订单状态为success
async def success_odd(request):

    # operator_id = request.args.get('operator_id')
    odd_id = request.args.get('odd_id')

    result = await OddTable().success_odd(odd_id)
    # print(result)
    return response.json(result)

@app.route("/change_odd_remark", methods=['POST'])#修改订单remark
async def change_odd_remark(request):

    odd_id = request.form.get('odd_id')
    remark = request.form.get('remark')

    result = await OddTable().change_remark(odd_id, remark)
    # print(result)
    return response.json(result)

@app.route("/select_odd_by_status", methods=['GET'])#
async def change_odd_remark(request):

    status = request.args.get('status')
    page = request.args.get('page')

    result = await OddTable().select_odd_by_status(status, page)
    # print(result)
    return response.json(result)

@app.route("/select_all_odd", methods=['GET'])#查看所有view_odd
async def change_odd_remark(request):

    result = await OddTable().select_all_odd()
    # print(result)
    return response.json(result)


@app.route("/select_all_odd_by_duty", methods=['POST'])#查看所有view_odd
async def change_odd_remark(request):

    duty_id = request.form.get('duty_id')
    result = await OddTable().select_all_odd_by_duty(duty_id)
    # print(result)
    return response.json(result)

@app.route("/select_odd_detail", methods=['GET'])#查看所有view_odd
async def change_odd_remark(request):

    odd_id = request.args.get('odd_id')
    result = await OddTable().odd_detail(odd_id)
    # print(result)
    return response.json(result)


######################### sku_account 入库 #######################################

@app.route("/add_sku_account_in", methods=['POST'])#增加入库单产品条目
async def add_sku_account_in(request):

    # sku_id, storage_location_id,quantity, odd_id, price

    odd_id = request.form.get('odd_id')
    sku_id = request.form.get('sku_id')
    storage_location_id = request.form.get('storage_location_id')
    quantity = request.form.get('quantity')
    price = request.form.get('price')

    result = await SkuAccountTable().add_sku_account_in(sku_id, storage_location_id,quantity, odd_id, price)
    # print(result)
    return response.json(result)

@app.route("/add_sku_account_out", methods=['POST'])#增加出库单产品条目
async def add_sku_account_in(request):

    # sku_id, storage_location_id,quantity, odd_id, price

    odd_id = request.form.get('odd_id')
    sku_id = request.form.get('sku_id')
    storage_location_id = request.form.get('storage_location_id')
    quantity = request.form.get('quantity')
    price = request.form.get('price')

    result = await SkuAccountTable().add_sku_account_out(sku_id, storage_location_id,quantity, odd_id, price)
    # print(result)
    return response.json(result)

@app.route("/update_sku_account_in", methods=['POST'])#更新入库单产品条目
async def update_sku_account_in(request):

    sku_account_id = request.form.get('sku_account_id')
    odd_id = request.form.get('odd_id')
    sku_id = request.form.get('sku_id')
    storage_location_id = request.form.get('storage_location_id')
    quantity = request.form.get('quantity')
    price = request.form.get('price')


    result = await SkuAccountTable().update_sku_account_in(sku_account_id, sku_id, storage_location_id,quantity, odd_id, price)
    # print(result)
    return response.json(result)

@app.route("/sku_account_success_in", methods=['POST'])#更新入库单产品条目status为success
async def sku_account_success_in(request):

    sku_account_id = request.form.get('sku_account_id')

    result = await SkuAccountTable().sku_account_success_in(sku_account_id)
    # print(result)
    return response.json(result)

#未测试
@app.route("/select_sku_account_by_odd_id", methods=['GET'])#查看指定odd的所有sku_account内容
async def select_sku_account_by_odd_id(request):

    odd_id = request.args.get('odd_id')

    result = await SkuAccountTable().select_sku_account_by_odd_id(odd_id)
    # print(result)
    return response.json(result)  

#sku库存情况出库和入库 未测试
@app.route("/select_sum_sku_account", methods=['GET'])
async def select_sum_sku_account(request):

    result = await SkuAccountTable().select_all_sum_view_sku_account()
    # print(result)
    return response.json(result)

# 查看sku_account by house_id and sku_id
@app.route("/select_view_sku_account__by_house_id_and_sku_id", methods=['GET'])
async def select_sum_sku_account(request):

    house_id = request.args.get('house_id')

    sku_id = request.args.get('sku_id')

    result = await SkuAccountTable().select_view_sku_account__by_house_id_and_sku_id(house_id, sku_id)
    # print(result)
    return response.json(result)

#sku库存情况出库和入库 未测试
@app.route("/select_sum_sku_account_by_sku_id", methods=['GET'])
async def select_sum_sku_account(request):

    sku_id = request.args.get('sku_id')
    
    result = await SkuAccountTable().select_all_sum_view_sku_account_by_sku_id(sku_id)
    # print(result)
    return response.json(result) 

#sku中库中详细情况，总量，和每个货位情况 未测试
@app.route("/select_sku_in_where_location", methods=['GET'])
async def select_sku_in_where_location(request):

    warehouse_id = request.args.get('warehouse_id')
    sku_id = request.args.get('sku_id')

    result = await SkuAccountTable().select_sku_in_where_location(warehouse_id, sku_id)
    # print(result)
    return response.json(result) 

###############权限验证#####################

#添加duty和class一类 的ship
@app.route("/add_duty_class_ship", methods=['POST'])
async def add_duty_class_ship(request):

    duty_id = request.form.get('duty_id')
    class_id = request.form.get('class_id')

    result = await DutyClassTable().add_ship(duty_id, class_id)
    # print(result)
    return response.json(result) 

#查看所有duty和class一类 的ship
@app.route("/select_all_class_ship", methods=['GET'])
async def select_all_class_ship(request):

    result = await DutyClassTable().select_all_ship()
    # print(result)
    return response.json(result) 

#删除duty和class一类 的ship
@app.route("/del_duty_class_ship", methods=['POST'])
async def del_duty_class_ship(request):

    duty_id = request.form.get('duty_id')
    class_id = request.form.get('class_id')

    result = await DutyClassTable().del_ship(duty_id, class_id)
    # print(result)
    return response.json(result) 

#----------------------duty_warehouse_ship------------------
#添加duty和warehouse 的ship
@app.route("/add_duty_warehouse_ship", methods=['POST'])
async def add_duty_warehouse_ship(request):

    duty_id = request.form.get('duty_id')
    warehouse_id = request.form.get('warehouse_id')

    result = await DutyWarehouseTable().add_ship(duty_id, warehouse_id)
    # print(result)
    return response.json(result)

#查看所有duty和warehouse
@app.route("/select_all_duty_warehouse_ship", methods=['GET'])
async def select_all_duty_warehouse_ship(request):

    result = await DutyWarehouseTable().select_all_ship()
    # print(result)
    return response.json(result) 



#删除duty和warehouse的ship
@app.route("/del_duty_warehouse_ship", methods=['POST'])
async def del_duty_class_ship(request):

    duty_id = request.form.get('duty_id')

    warehouse_id = request.form.get('warehouse_id')

    result = await DutyWarehouseTable().del_ship(duty_id, warehouse_id)
    # print(result)
    return response.json(result)

#更新duty和warehouse的ship
@app.route("/update_duty_warehouse_ship", methods=['POST'])
async def update_duty_warehouse_ship(request):

    #  in_operation, out_operation,

    duty_id = request.form.get('duty_id')
    warehouse_id = request.form.get('warehouse_id')
    out = request.form.get('out')
    in_ = request.form.get('in_')
    out_odd = request.form.get('out_odd')
    in_odd = request.form.get('in_odd')
    out_operation = request.form.get('out_operation')
    in_operation = request.form.get('in_operation')
    examine = request.form.get('examine')

    result = await DutyWarehouseTable().updte_ship(
                        duty_id, warehouse_id, 
                        out, in_ ,out_odd, in_odd, 
                        in_operation, out_operation, examine)
    # print(result)
    return response.json(result)

@app.route("/select_all_sku_by_duty_id", methods=['POST'])#查看所有sku
async def select_all_warehose(request):

    duty_id = request.form.get('duty_id')
    result = await DutyClassTable().select_all_sku_by_duty_id(duty_id)
    # print(result)
    return response.json(result)


@app.route("/select_all_class_by_duty_id", methods=['POST'])#查看所有sku
async def select_all_warehose(request):

    duty_id = request.form.get('duty_id')
    result = await DutyClassTable().select_all_class_by_duty_id(duty_id)
    # print(result)
    return response.json(result)

#查看库房所有sku详细情况
@app.route("/select_all_sku_detail_by_warehouse_id", methods=['POST'])
async def select_all_sku_detail_by_warehouse_id(request):

    warehouse_id = request.form.get('warehouse_id')
    result = await SkuWarehouseData().select_all_sku_detail_by_warehouse_id(warehouse_id)
    # print(result)
    return response.json(result)

#移位单 不是携程
@app.route("/move_sku_location", methods=['POST'])
def move_sku_location(request):

    # from_location_id, to_location_id, sku_id, value

    from_location_id = request.form.get('from_location_id')
    to_location_id = request.form.get('to_location_id')
    sku_id = request.form.get('sku_id')
    value = request.form.get('value') #数量
    result = MoveSkuLocation().move_sku_location(from_location_id, to_location_id, sku_id, value)
    # print(result)
    return response.json(result)

#删除未完成的odd订单
@app.route("/del_odd", methods=['POST'])
async def del_odd(request):

    odd_id = request.form.get('odd_id')

    result = await OddTable().del_odd(odd_id)
    print(result)
    return response.json(result)

if __name__ == "__main__":

    app.run(host="0.0.0.0", port=8000, debug=True)


